package com.lucky.yingying

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import java.util.*

@RestController
@RequestMapping("/api/luck")
class LuckyService {

    fun okMsg(city: String) = "欢迎参加xxx年会，恭喜您抽中了现场参与名额，参与地点为${getPlace(city)}多功能厅~"

    fun failMsg(city: String) = "欢迎参加xxx年会，2022年1月25日15：00 让我们在直播间云端相聚"

    @GetMapping("/get")
    fun get(@RequestParam("id") id: Int): APIResult<Lucky> {
        val one = NameList.nameList[id]
        return APIResult("", true, one)
    }

    @GetMapping("/chou")
    fun luck(@RequestParam("id") id: Int): APIResult<Lucky> {
        val one = NameList.nameList[id]
        print("timestamp=${Date()}, id=$id, one=${one} | ")

        // 参数校验
        val city: String = one.city

        // 白名单逻辑: 南京的默认都去现场
        if ("南京" == city) {
            one.chou = true
            one.lucky = true
            println("抽中!")
            return APIResult(okMsg(city), true, one)
        }

        val total_luck: Int = getTotalLuck(city)
        val thisCityLuckCount = NameList.nameList.filter { it.city == city && it.lucky == true }.size

        if (thisCityLuckCount >= total_luck) {
            println("门票已经抽完啦!")
            return APIResult(failMsg(city), false, one)
        }

        // 重复 chou
        if (one.chou && !one.lucky) {
            println("已经抽过没抽中!")
            return APIResult(failMsg(city), false, one)
        }

        if (one.chou && one.lucky) {
            println("已经抽过且抽中!")
            return APIResult(okMsg(city), true, one)
        }

        // 第一次 chou
        if (!one.chou) {
            val lucky = getRandomBoolean()
            // 只要逻辑走到了这里, 就把 chou 设置为 true 了.
            one.chou = true

            if (lucky) {
                one.lucky = true
                println("抽中!")
                return APIResult(okMsg(city), true, one)
            } else {
                println("没抽中!")
                return APIResult(failMsg(city), false, one)
            }
        }

        // 异常逻辑
        return APIResult(failMsg(city), false, one)

    }


    private fun getTotalLuck(city: String): Int {
        return when (city) {
            "成都" -> CHENGDU_TOTAL_LUCK
            "南京" -> NANJING_TOTAL_LUCK
            "南昌" -> NANCHANG_TOTAL_LUCK
            "武汉" -> WUHAN_TOTAL_LUCK
            "重庆" -> CHONGQING_TOTAL_LUCK
            else -> -1
        }
    }

    private fun getLuckies(city: String): List<Lucky> {
        return when (city) {
            "成都" -> NameList.nameList.filter { it.city == "成都" && it.lucky == true }
            "南京" -> NameList.nameList.filter { it.city == "南京" && it.lucky == true }
            "南昌" -> NameList.nameList.filter { it.city == "南昌" && it.lucky == true }
            "武汉" -> NameList.nameList.filter { it.city == "武汉" && it.lucky == true }
            "重庆" -> NameList.nameList.filter { it.city == "重庆" && it.lucky == true }
            else -> emptyList()
        }
    }

    @GetMapping("/luckyList")
    fun luckList(): APIResult<List<Lucky>> {
        return APIResult("", true, NameList.nameList.filter { it.lucky })
    }

    @GetMapping("/allList")
    fun allList(): APIResult<List<Lucky>> {
        return APIResult("", true, NameList.nameList)
    }

    @GetMapping("/listByCity")
    fun luckList(@RequestParam("city") city: String): APIResult<Map<String, Lucky>> {
        return APIResult("", true, getLuckies(city))
    }

    fun getRandomBoolean(): Boolean {
        val random = Random()
        return random.nextBoolean()
    }

}

